<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Handling Failures in Pipelines | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'handling-failure-in-pipelines.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/handling-failure-in-pipelines.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/handling-failure-in-pipelines.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/handling-failure-in-pipelines.html" rel="nofollow" target="_blank">../en/handling-failure-in-pipelines.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="ingest.html">Ingest node</a></span>
»
<span class="breadcrumb-node">Handling Failures in Pipelines</span>
</div>
<div class="navheader">
<span class="prev">
<a href="conditionals-with-regex.html">« Conditionals with the Regular Expressions</a>
</span>
<span class="next">
<a href="ingest-enriching-data.html">Enrich your data »</a>
</span>
</div>
<div class="chapter">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="handling-failure-in-pipelines"></a>Handling Failures in Pipelines<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/ingest-node.asciidoc">edit</a>
</h2>
</div></div></div>
<p>In its simplest use case, a pipeline defines a list of processors that
are executed sequentially, and processing halts at the first exception. This
behavior may not be desirable when failures are expected. For example, you may have logs
that don’t match the specified grok expression. Instead of halting execution, you may
want to index such documents into a separate index.</p>
<p>To enable this behavior, you can use the <code class="literal">on_failure</code> parameter. The <code class="literal">on_failure</code> parameter
defines a list of processors to be executed immediately following the failed processor.
You can specify this parameter at the pipeline level, as well as at the processor
level. If a processor specifies an <code class="literal">on_failure</code> configuration, whether
it is empty or not, any exceptions that are thrown by the processor are caught, and the
pipeline continues executing the remaining processors. Because you can define further processors
within the scope of an <code class="literal">on_failure</code> statement, you can nest failure handling.</p>
<p>The following example defines a pipeline that renames the <code class="literal">foo</code> field in
the processed document to <code class="literal">bar</code>. If the document does not contain the <code class="literal">foo</code> field, the processor
attaches an error message to the document for later analysis within
Elasticsearch.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "description" : "my first pipeline with handled exceptions",
  "processors" : [
    {
      "rename" : {
        "field" : "foo",
        "target_field" : "bar",
        "on_failure" : [
          {
            "set" : {
              "field" : "error",
              "value" : "field \"foo\" does not exist, cannot rename to \"bar\""
            }
          }
        ]
      }
    }
  ]
}</pre>
</div>
<p>The following example defines an <code class="literal">on_failure</code> block on a whole pipeline to change
the index to which failed documents get sent.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "description" : "my first pipeline with handled exceptions",
  "processors" : [ ... ],
  "on_failure" : [
    {
      "set" : {
        "field" : "_index",
        "value" : "failed-{{ _index }}"
      }
    }
  ]
}</pre>
</div>
<p>Alternatively instead of defining behaviour in case of processor failure, it is also possible
to ignore a failure and continue with the next processor by specifying the <code class="literal">ignore_failure</code> setting.</p>
<p>In case in the example below the field <code class="literal">foo</code> doesn’t exist the failure will be caught and the pipeline
continues to execute, which in this case means that the pipeline does nothing.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "description" : "my first pipeline with handled exceptions",
  "processors" : [
    {
      "rename" : {
        "field" : "foo",
        "target_field" : "bar",
        "ignore_failure" : true
      }
    }
  ]
}</pre>
</div>
<p>The <code class="literal">ignore_failure</code> can be set on any processor and defaults to <code class="literal">false</code>.</p>
<h3>
<a id="accessing-error-metadata"></a>Accessing Error Metadata From Processors Handling Exceptions<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/ingest-node.asciidoc">edit</a>
</h3>
<p>You may want to retrieve the actual error message that was thrown
by a failed processor. To do so you can access metadata fields called
<code class="literal">on_failure_message</code>, <code class="literal">on_failure_processor_type</code>, <code class="literal">on_failure_processor_tag</code> and
<code class="literal">on_failure_pipeline</code> (in case an error occurred inside a pipeline processor).
These fields are only accessible from within the context of an <code class="literal">on_failure</code> block.</p>
<p>Here is an updated version of the example that you
saw earlier. But instead of setting the error message manually, the example leverages the <code class="literal">on_failure_message</code>
metadata field to provide the error message.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "description" : "my first pipeline with handled exceptions",
  "processors" : [
    {
      "rename" : {
        "field" : "foo",
        "to" : "bar",
        "on_failure" : [
          {
            "set" : {
              "field" : "error",
              "value" : "{{ _ingest.on_failure_message }}"
            }
          }
        ]
      }
    }
  ]
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="conditionals-with-regex.html">« Conditionals with the Regular Expressions</a>
</span>
<span class="next">
<a href="ingest-enriching-data.html">Enrich your data »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>